//
//  WTRecomentListTwoCell.m
//  OneDay
//
//  Created by lanou on 16/1/12.
//  Copyright © 2016年 小明. All rights reserved.
//

#import "WTRecommendListTwoCell.h"
#import <UIImageView+WebCache.h>

// 距离边框的长度
#define kMargin (20 *  kRelativeLength_X)
// 图片的长度
#define kPicOneWidth (kScreenWidth - 4 * kMargin) / 3

// 图片的高度
#define kPicOneHeight (80 *  kRelativelength_Y)

// 图片距离标题的
#define kPicToTitle 10 * kRelativelength_Y
// 热门,推荐这个图片的长度
#define kMarkImageWidth 15 * kRelativeLength_X

// TimeAgoLabel的长度
#define kTimeAgoLabelWidth 100 * kRelativeLength_X

// commentLabel的长度
#define kCommentLabelWidth 100 * kRelativeLength_X

@implementation WTRecommendListTwoCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self addSubviews];
    }
    return self;
}

// 添加子控件
- (void)addSubviews
{
    
    // 标题
    _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(kMargin, kMargin, kScreenWidth - 2 * kMargin, kMargin)];
    _titleLabel.numberOfLines = -1;
    _titleLabel.font = [UIFont systemFontOfSize:16];
    [self.contentView  addSubview:_titleLabel];
    
    
    // 第一张图片
    _picOneImageV = [[UIImageView alloc]initWithFrame:CGRectMake(_titleLabel.left, _titleLabel.bottom + kPicToTitle, kPicOneWidth, kPicOneHeight)];
    _picOneImageV.backgroundColor = [UIColor redColor];
    [self.contentView  addSubview:_picOneImageV];
    
    // 第二张图片
    _picTwoImageV = [[UIImageView alloc]initWithFrame:CGRectMake(_picOneImageV.right + kMargin, _picOneImageV.top, _picOneImageV.width, _picOneImageV.height)];
    _picTwoImageV.backgroundColor = [UIColor redColor];
    [self.contentView  addSubview:_picTwoImageV];
    
    // 第三张图片
    _picThrImageV = [[UIImageView alloc]initWithFrame:CGRectMake(_picTwoImageV.right + kMargin, _picOneImageV.top, _picOneImageV.width, _picOneImageV.height)];
    _picThrImageV.backgroundColor = [UIColor redColor];
    [self.contentView  addSubview:_picThrImageV];
    
    
    
    
    // 推荐或者热点状态
    _markImagView = [[UIImageView alloc]initWithFrame:CGRectMake(_titleLabel.left, _picOneImageV.bottom + kPicToTitle, kMarkImageWidth, kMarkImageWidth)];
    [self.contentView  addSubview:_markImagView];
    
    // 多长时间前
    _timeAgoLabel = [[UILabel alloc]initWithFrame:CGRectMake(_markImagView.right + kPicToTitle, _markImagView.top, kTimeAgoLabelWidth, _markImagView.height)];
    _timeAgoLabel.font = [UIFont systemFontOfSize:14];
    [self.contentView  addSubview:_timeAgoLabel];
    
    
    // 评论
    _commentLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth - kCommentLabelWidth - kMargin, _timeAgoLabel.top, kCommentLabelWidth, _timeAgoLabel.height)];
    _commentLabel.font = [UIFont systemFontOfSize:14];
    _commentLabel.textAlignment = NSTextAlignmentRight;
    [self.contentView  addSubview:_commentLabel];
    
    
    
    _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, _commentLabel.bottom + kMargin - kRelativelength_Y, kScreenWidth, kRelativelength_Y)];
    _bottomView.backgroundColor = [UIColor blackColor];
    [self.contentView addSubview:_bottomView];
}


- (void)setModel:(WTRecommendListModel *)model
{
    _model = model;

   
    // title自适应高度

    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16],NSFontAttributeName, nil];
    
    CGRect fram = [model.title boundingRectWithSize:CGSizeMake(_titleLabel.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    _titleLabel.frame = CGRectMake(kMargin, kMargin,kScreenWidth - 2 * kMargin,fram.size.height);
    
    
    // 对有几张图片进行判断,一张的另一个cell,0张是一种,3张是一种情况
    // 说明没有图片
    if ([model.picThr isEqualToString:@""]) {
        // 没有图片时,将这三张图片的imageView高度设置为0
        _picOneImageV.frame = CGRectMake(_picOneImageV.left, _titleLabel.bottom + kPicToTitle, kScreenWidth, 0);
        _picTwoImageV.frame = CGRectMake(_picOneImageV.right + kMargin, _picOneImageV.top,_picOneImageV.width,0);
        _picThrImageV.frame = CGRectMake(_picTwoImageV.right + kMargin, _picOneImageV.top,_picOneImageV.width,0);
    }else {
        // 第一张图片
        _picOneImageV.frame = CGRectMake(_titleLabel.left, _titleLabel.bottom + kPicToTitle, kPicOneWidth, kPicOneHeight);
      
        
        // 第二张图片
        _picTwoImageV.frame = CGRectMake(_picOneImageV.right + kMargin, _picOneImageV.top, _picOneImageV.width, _picOneImageV.height);
       
        // 第三张图片
        _picThrImageV.frame = CGRectMake(_picTwoImageV.right + kMargin, _picOneImageV.top, _picOneImageV.width, _picOneImageV.height);
        
        
        // 异步加载图片
        [_picOneImageV sd_setImageWithURL:[NSURL URLWithString:model.picOne]placeholderImage:[UIImage imageNamed:@"m-zanweitu"]];
        [_picTwoImageV sd_setImageWithURL:[NSURL URLWithString:model.picTwo] placeholderImage:[UIImage imageNamed:@"m-zanweitu"]];
        [_picThrImageV sd_setImageWithURL:[NSURL URLWithString:model.picThr] placeholderImage:[UIImage imageNamed:@"m-zanweitu"]];
    }
    
    // 对是不是推荐,热门进行判断,1是推荐,2是热门
    if ([model.mark isEqualToString:@"1"]) {
        _markImagView.image = [UIImage imageNamed:@"m-tuijian"];
        _markImagView.frame = CGRectMake(_titleLabel.left, _picOneImageV.bottom + kPicToTitle, kMarkImageWidth, kMarkImageWidth);
        _timeAgoLabel.frame = CGRectMake(_markImagView.right + kPicToTitle , _markImagView.top, kTimeAgoLabelWidth, _markImagView.height);
        
    } else if ([model.mark isEqualToString:@"2"]){
        _markImagView.image = [UIImage imageNamed:@"m-remen"];
        _markImagView.frame = CGRectMake(_titleLabel.left, _picOneImageV.bottom + kPicToTitle, kMarkImageWidth, kMarkImageWidth);
        _timeAgoLabel.frame = CGRectMake(_markImagView.right + kPicToTitle , _markImagView.top, kTimeAgoLabelWidth, _markImagView.height);
    }else {
        _markImagView.frame = CGRectMake(_markImagView.left, _markImagView.top, 0, _markImagView.height);
        _timeAgoLabel.frame = CGRectMake(_markImagView.right, _picOneImageV.bottom + kPicToTitle, kTimeAgoLabelWidth, _markImagView.height);
        
    }
    
    _titleLabel.text = model.title;
    _timeAgoLabel.text = model.timeAgo;
    
    _commentLabel.frame = CGRectMake(kScreenWidth - kCommentLabelWidth - kMargin, _timeAgoLabel.top, kCommentLabelWidth, _timeAgoLabel.height);
    _commentLabel.text = [NSString stringWithFormat:@"%@评论",model.commentNum];
 
    _bottomView.frame = CGRectMake(0, _commentLabel.bottom + kMargin - kRelativelength_Y, kScreenWidth, kRelativelength_Y);
}

// 返回cell的高度
+ (CGFloat)heightForRow:(WTRecommendListModel *)model
{
    // title自适应高度
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16],NSFontAttributeName, nil];
    
    CGRect fram = [model.title boundingRectWithSize:CGSizeMake(kScreenWidth - 2 * kMargin, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    // 判断是不是有图片
    if ([model.picThr isEqualToString:@""]) {
        return kMargin + fram.size.height + kPicToTitle + kMarkImageWidth + kMargin;
    }else {
        return kMargin + fram.size.height + kPicToTitle  + kPicOneHeight + kPicToTitle+ kMarkImageWidth + kMargin;
    }

}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
